package string;


import java.util.Random;
import java.util.Scanner;

/*
生成一个4位验证码(数字和字母的组合)，输出到控制台并提示用户输入验证码，输入后若正确则提示验证码正确，若错误则提示验证码错误。注意：不区分大小写
 */
public class Exercises1 {
    public static void main(String[] args) {
        String line = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01923456789";
        String code = "";
        Random rand = new Random();
        for(int i=1;i<=4;i++){
            int index = rand.nextInt(line.length());
            char c = line.charAt(index);
            code += c;
        }
        System.out.println("验证码为"+code);
        System.out.println("请输入验证码：");
        Scanner scan = new Scanner(System.in);
        String input = scan.nextLine();
        String i = input.toUpperCase(); //转换为小写的也可以
        String c = code.toUpperCase(); //转换为小写的也可以
        if(i.equals(c)){
            System.out.println("验证成功");
        }else{
            System.out.println("验证失败");
        }

        /*不区分大小写的另一种方法
        if(input.equalsIgnoreCase(code)){
            System.out.println("验证成功");
        }else{
            System.out.println("验证失败");
        }
        */
    }
}
